<?php

declare(strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Controller\business;

use App\Middleware\User\UserMiddleware;
use App\Request\Business\Business\ChangeBusinessInfoRequest;
use App\Request\Business\Business\ChangeFullDiscountRateRequest;
use App\Request\Business\Business\ChangeJoiningWayRequest;
use App\Request\Business\Business\ChangeInfoRequest;
use App\Request\Business\Cert\BusinessCertRequest;
use App\Service\Business\BusinessService;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;

/**
 * 商家控制器
 *
 * @AutoController()
 * @Middleware(UserMiddleware::class)
 * @author 李想(928674263@qq.com)
 * @package App\Controller\business
 */
class BusinessController extends Controller
{
    /**
     * 商家服务
     *
     * @Inject
     * @var BusinessService
     */
    private $service;

    /**
     * 获取商家审核失败结果
     */
    public function getBusinessAuditResult()
    {
        $result = $this->service->dao->first(['user_id' => uid()],['id','user_id','status','audit_result']);

        if( $result['status'] == '3' ) {
            $return = [
                'result' => $result['audit_result'],
                'status' => $result['status']
            ];
        }
        if( $result['status'] == '2' ) {
            $return = [
                'result' => "待审核",
                'status' => $result['status']
            ];
        }

        $result ?$this->success($result):$this->error();
    }

    /**
     * 获取不通过的商家信息
     */
    public function getFailBusiness()
    {
        $result = $this->service->getFailBusiness();

        $this->success($result);
    }

    /**
     * 获取登录商家状态
     */
    public function get_status()
    {
        $status = $this->service->getBusinessStatus();

        $this->success($status);
    }

    /**
     * 获取商家ID -- 认证店铺之后
     */
    public function getBusinessId()
    {
        $business = $this->service->dao->first([
            'user_id'=>uid()
        ],['id']);

        $this->success( $business['id'] );
    }

    /**
     * 商家认证接口
     *
     * @param BusinessCertRequest $request
     * @throws
     */
    public function cert(BusinessCertRequest $request)
    {
        $params = $request->all();

        isset($params['province_id']) && $params['province_id'] = (int)$params['province_id'];//省
        isset($params['city_id']) && $params['city_id'] = (int)$params['city_id'];//市
        isset($params['district_id']) && $params['district_id'] = (int)$params['district_id'];//区
        isset($params['longitude']) && $params['longitude'] = (float)$params['longitude'];
        isset($params['latitude']) && $params['latitude'] = (float)$params['latitude'];
        isset($params['identity_code']) && $params['identity_code'] = (string)$params['identity_code'];
//        isset($params['full_discount_type']) && $params['full_discount_type'] = (int)$params['full_discount_type'];
        isset($params['settled_type']) && $params['settled_type'] = (int)$params['settled_type'];//入驻类型1全店打折，2优惠券，3现金抵扣
        isset($params['industry_id']) && $params['industry_id'] = (int)$params['industry_id'];//行业类别
        isset($params['business_bank_id']) && $params['business_bank_id'] = (int)$params['business_bank_id'];//银行卡关联id

        $result = $this->service->cert($params);

        $this->success($result);
    }

    /**
     * 获取商家加盟方式
     */
    public function get_joining_way()
    {
        $joining_way = $this->service->getJoiningWay();

        $this->success($joining_way);
    }

    /**
     * 更改商家加盟方式
     *
     * @param ChangeJoiningWayRequest $request
     */
    public function change_joining_way(ChangeJoiningWayRequest $request)
    {
        $params = $request->all();

        isset($params['enable_full_discount']) && $params['enable_full_discount'] = (int)$params['enable_full_discount'];
        isset($params['enable_coupon']) && $params['enable_coupon'] = (int)$params['enable_coupon'];
        isset($params['enable_voucher']) && $params['enable_voucher'] = (int)$params['enable_voucher'];
        
        $this->service->changeJoiningWay($params);

        $this->success();
    }

    /**
     * 获取商家全店打折比例
     */
    public function get_full_discount_rate()
    {
        $rate = $this->service->getFullDiscountRate();

        $this->success($rate);
    }

    /**
     * 更改全店打折比例
     *
     * @param ChangeFullDiscountRateRequest $request
     */
    public function change_full_discount_rate(ChangeFullDiscountRateRequest $request)
    {
        $params = $request->all();

        isset($params['full_discount_rate']) && $params['full_discount_rate'] = (float)$params['full_discount_rate'];

        $this->service->changeFullDiscountRate($params);

        $this->success();
    }

    /**
     * 获取商家信息
     */
    public function get_info()
    {
        $result = $this->service->getInfo();

        $this->success($result);
    }

    /**
     * 修改商家信息
     *
     * @param ChangeBusinessInfoRequest $request
     */
    public function change_info(ChangeBusinessInfoRequest $request)
    {
        $params = $request->all();

        isset($params['province_id']) && $params['province_id'] = (int)$params['province_id'];
        isset($params['city_id']) && $params['city_id'] = (int)$params['city_id'];
        isset($params['district_id']) && $params['district_id'] = (int)$params['district_id'];
        isset($params['industry_id']) && $params['industry_id'] = (int)$params['industry_id'];
        isset($params['address']) && $params['address'] = (string)$params['address'];
        isset($params['longitude']) && $params['longitude'] = (float)$params['longitude'];
        isset($params['latitude']) && $params['latitude'] = (float)$params['latitude'];
        isset($params['business_cover']) && $params['business_cover'] = (string)$params['business_cover'];

        $this->service->changeInfo($params);

        $this->success();
    }

    /**
     * 查询商家信息是否已经完善---用于商家信息完善
     */
    public function is_complete()
    {
        $result = $this->service->getInfo();

        $return['id'] = $result['business']['id'];//商家id
        $return['industry_id'] = $result['business']['industry_id'];//商家行业ID
        $return['industry_name'] = $result['industry_name'];//商家行业ID
        $return['group_id'] = $result['group']['id'];//会员等级id
        $return['group_name'] = $result['group']['name'];//会员等级
        $return['province_id'] = $result['business']['province_id'];//省id
        $return['province'] = $result['business']['province'];
        $return['city_id'] = $result['business']['city_id'];//市id
        $return['city'] = $result['business']['city'];
        $return['district_id'] = $result['business']['district_id'];//区id
        $return['district'] = $result['business']['district'];//
        $return['longitude'] = $result['business']['longitude'];//经度
        $return['latitude'] = $result['business']['latitude'];//纬度
        $return['location'] = $result['business']['location'] ?? '';//定位位置
        $return['logo'] = $result['business']['logo'] ?? '';//logo
        $return['phone'] = $result['phone'];//用户账号
        $return['name'] = $result['business']['name'];//店铺名称
        $return['description'] = $result['business']['description'] ?? '';//店铺介绍
        $return['science'] = $result['business']['science'] ?? [];//店铺环境
        $return['tel'] = $result['business']['tel'];//店铺电话
        $return['address'] = $result['business']['address'];//店铺地址
        $return['forenoon_hours'] = $result['business']['forenoon_hours'] ?? '';//上午营业时间
        $return['afternoon_hours'] = $result['business']['afternoon_hours'] ?? '';//下午营业时间
        $return['hours_describe'] = $result['business']['hours_describe'];//营业时间描述
        $return['business_cover'] = $result['business']['business_cover'];//店铺封面
//        $return['tag'] = is_null($result['business']['tag']) ? [] : json_decode($result['business']['tag'],true);//标签
        $return['tag'] = $result['business']['tag'];//标签
        $return['type'] = $result['business']['type'];//店铺入驻方式
        $return['province_pid'] = $result['business']['province_pid'];//店铺入驻方式
        $return['city_pid'] = $result['business']['city_pid'];//店铺入驻方式
        $return['district_pid'] = $result['business']['district_pid'];//店铺入驻方式

        $this->success($return);
    }

}